/*
*  Copyright 2019-2020 Liu
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.liu.modules.business.rest;


import cn.hutool.core.util.IdUtil;
import com.liu.annotation.AnonymousAccess;
import com.liu.annotation.Log;
import com.liu.exception.BadRequestException;
import com.liu.modules.business.domain.EjlCusUserInfo;
import com.liu.modules.business.service.EjlCusUserInfoService;
import com.liu.modules.business.service.dto.EjlCusUserInfoDto;
import com.liu.modules.business.service.dto.EjlCusUserInfoQueryCriteria;
import com.liu.utils.QueryHelp;
import com.liu.utils.RedisUtils;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

/**
* @website https://el-admin.vip
* @author Liu
* @date 2022-07-03
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "注册用户管理管理")
@RequestMapping("/api/ejlCusUserInfo")
public class EjlCusUserInfoController {

    private final EjlCusUserInfoService ejlCusUserInfoService;
    private RedisUtils redisUtils;


    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('ejlCusUserInfo:list')")
    public void download(HttpServletResponse response, EjlCusUserInfoQueryCriteria criteria) throws IOException {
        ejlCusUserInfoService.download(ejlCusUserInfoService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询注册用户管理")
    @ApiOperation("查询注册用户管理")
    @AnonymousAccess
    public ResponseEntity<Object> query(EjlCusUserInfoQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(ejlCusUserInfoService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @Log("用户登录")
    @ApiOperation("用户登录")
    @PostMapping(value = "/login")
    @AnonymousAccess
    public ResponseEntity<Object> login(@RequestBody Map<String,String> userInfo){
        EjlCusUserInfoQueryCriteria criteria = new EjlCusUserInfoQueryCriteria();
        criteria.setUsename(userInfo.get("usename"));
        criteria.setUserPassword(userInfo.get("userPassword"));
        List<EjlCusUserInfoDto> ejlCusUserInfoDtoList = ejlCusUserInfoService.queryAll(criteria);
        if(ejlCusUserInfoDtoList.size() <= 0 ){
            throw new BadRequestException("账户密码不正确");
        }
        return new ResponseEntity<>(ejlCusUserInfoDtoList.get(0),HttpStatus.OK);
    }

    @Log("用户注册")
    @ApiOperation("用户注册")
    @PostMapping(value = "/register")
    @AnonymousAccess
    public ResponseEntity<Object> create(@RequestBody Map<String,Object> userMap){
        return new ResponseEntity<>(ejlCusUserInfoService.register(userMap),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增注册用户管理")
    @ApiOperation("新增注册用户管理")
    @AnonymousAccess
    public ResponseEntity<Object> create(@Validated @RequestBody EjlCusUserInfo resources){
        return new ResponseEntity<>(ejlCusUserInfoService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改注册用户管理")
    @ApiOperation("修改注册用户管理")
    @PreAuthorize("@el.check('ejlCusUserInfo:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody EjlCusUserInfo resources){
        ejlCusUserInfoService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除注册用户管理")
    @ApiOperation("删除注册用户管理")
    @PreAuthorize("@el.check('ejlCusUserInfo:del')")
    @DeleteMapping
    public ResponseEntity<Object> delete(@RequestBody String[] ids) {
        ejlCusUserInfoService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}